﻿using System.ComponentModel;

namespace Kit3D.Windows.Media
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    [TypeConverter(typeof(Int32Collection.TypeConverter))]
    public class Int32Collection : Freezable, IList, IList<Int32>
    {
        private List<Int32> items;

        public Int32Collection()
        {
            this.items = new List<int>();
        }

        public Int32Collection(int capacity)
        {
            this.items = new List<int>(capacity);
        }

        public Int32Collection(IEnumerable<Int32> collection)
        {
            this.items = new List<int>(collection);
        }

        #region ICollection<int> Members

        public void Add(int item)
        {
            Insert(Count, item);
        }

        public int Add(object value)
        {
            var intValue = (int) value;
            Add(intValue);
            return IndexOf(intValue);
        }

        public bool Contains(object value)
        {
            return Contains((int) value);
        }

        public void Clear()
        {
            this.items.Clear();
            OnChanged();
        }

        public int IndexOf(object value)
        {
            return IndexOf((int) value);
        }

        public void Insert(int index, object value)
        {
            Insert(index, (int) value);
        }

        public void Remove(object value)
        {
            Remove((int) value);
        }

        public int IndexOf(int item)
        {
            return this.items.IndexOf(item);
        }

        public void Insert(int index, int item)
        {
            this.items.Insert(index, item);
            OnChanged();
        }

        void IList<int>.RemoveAt(int index)
        {
            Remove(this[index]);
        }

        int IList<int>.this[int index]
        {
            get { return this[index]; }
            set { this[index] = value; }
        }

        void IList.RemoveAt(int index)
        {
            Remove(this[index]);
        }

        object IList.this[int index]
        {
            get { return this[index]; }
            set { this[index] = (int) value; }
        }

        public bool Contains(int item)
        {
            return this.items.Contains(item);
        }

        public void CopyTo(int[] array, int arrayIndex)
        {
            this.items.CopyTo(array, arrayIndex);
        }

        public void CopyTo(Array array, int index)
        {
            CopyTo((int[]) array, index);
        }

        public int Count
        {
            get { return this.items.Count; }
        }

        private object _syncObject = new object();
        public object SyncRoot
        {
            get { return _syncObject; }
        }

        public bool IsSynchronized
        {
            get { return true; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool Remove(int item)
        {
            bool result = this.items.Remove(item);
            if (result)
            {
                OnChanged();
            }
            return result;
        }

        #endregion

        #region IEnumerable<int> Members

        public IEnumerator<int> GetEnumerator()
        {
            return this.items.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public Int32 this[int index]
        {
            get
            {
                return this.items[index];
            }
            set
            {
                if (index < Count)
                    Remove(this[index]);
                this.items[index] = value;
                OnChanged();
            }
        }

        public class TypeConverter : System.ComponentModel.TypeConverter
        {
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return (sourceType == typeof (string));
            }

            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                throw new NotImplementedException();
            }
        }
    }
}

